package com.kehutong.house.admin;
import com.kehutong.common.util.Token;

import com.kehutong.house.entity.CommunityMerchant;
import com.kehutong.house.entity.EquipmentClassify;
import com.kehutong.house.entity.EquipmentType;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.entity.FindPage;
import org.coraframework.util.Objects;

import java.util.List;

@MvcAction
@ReqMapping("/admin/equipment")
public class EquipmentTypeController {
    @Inject
    private JdbcSession jdbcSession;
    public final static String[] EXCEL_IMPORT_HEADER = {"设备类型名称"};
    @ReqMapping("/list")
    public Object list(Token token, JSONObject jsonObject) {
        FindPage<EquipmentType> find = jdbcSession.findPage(EquipmentType.class)
                .like(jsonObject, "deviceTypeName", "deviceTypeName")
                .eq("deleted", false)
                .order("typeStatus", "DESC")
                .order("sequence", "ASC")
                .page(jsonObject);
        Page<EquipmentType> page=find.exe();
        page.getList().forEach(item->{
            //查询分类
            List<EquipmentClassify>equipmentClassifyList=jdbcSession.findArray(EquipmentClassify.class).eq("deviceTypeId",item.getId())
                    .eq("deleted",false).eq("typeStatus",true).exe();
            item.setCount(equipmentClassifyList.size());
        });
        return page;
    }
    @ReqMapping("/listAll")
    public Object listAll(JSONObject jsonObject) throws Exception {
        return jdbcSession.findArray(EquipmentType.class)
                .eq("deleted", false)
                .eq("typeStatus", true)
                .order("createTime", "DESC")
                .exe();
    }
    @Bean(copy=true)
    @ReqMapping("/update")
    public Object update(EquipmentType equipmentType) throws Exception {
        // 增加校验不允许有相同的类型名称名称
        EquipmentType item = jdbcSession.findOne(EquipmentType.class)
                .eq("deviceTypeName", equipmentType.getDeviceTypeName())
                .noEq("id", equipmentType.getId())
                .eq("deleted", false)
                .exe();

        if(Objects.nonNull(item)){
            return Result.error(0, "已有当前设备类型名称，请修改后重试");
        }
        jdbcSession.updateById(equipmentType);
        return Result.success();
    }

    @Bean(copy=true)
    @ReqMapping("/updateSetStatus")
    public Object updateSetStatus(EquipmentType equipmentType) throws Exception {
        jdbcSession.updateById(equipmentType);
        return Result.success();
    }
    @ReqMapping("/delete")
    public Object delete(EquipmentType equipmentType) {
        if (equipmentType != null) {
            equipmentType.setDeleted(Boolean.TRUE);
            jdbcSession.updateById(equipmentType);
        }
        return Result.success();
    }

    @Bean(newInstance=true, copy=true)
    @ReqMapping("/save")
    public Object save(EquipmentType equipmentType) {

        // 增加校验不允许有相同的分类名称
        EquipmentType item = jdbcSession.findOne(EquipmentType.class)
                .eq("deviceTypeName", equipmentType.getDeviceTypeName())
                .eq("deleted", false)
                .exe();
        if(Objects.nonNull(item)){
            return Result.error(0, "已有当前类型名称，请修改后重试");
        }
        equipmentType.setAmount(0);
        jdbcSession.insert(equipmentType);
        return Result.success(equipmentType);
    }

    @ReqMapping("/get")
    public Object get(EquipmentType type) {
        return Result.success(type);
    }
}
